<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Logic\Api\ApplyLogic;
use App\Http\Requests\Api\Apply\Create;
use App\Http\Requests\Api\Apply\Detail;
use Exception;
use Illuminate\Http\JsonResponse;

class ApplyController extends Controller
{
    protected ApplyLogic $applyLogic;

    public function __construct(ApplyLogic $applyLogic)
    {
        $this->applyLogic = $applyLogic;
    }

    public function create(Create $create): JsonResponse
    {
        try {
            return $this->success(
                $this->applyLogic->create(
                    $create->validated(),
                    $this->getUser()
                )
            );
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    public function list(): JsonResponse
    {
        try {
            return $this->success(
                $this->applyLogic->list($this->getUser())
            );
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    public function detail(Detail $detail): JsonResponse
    {
        try {
            return $this->success(
                $this->applyLogic->detail(
                    $detail->validated()
                )
            );
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
    }
}
